package io.pumelo;

import io.pumelo.bug.Bug;
import io.pumelo.property.RemoteProperties;



/**
 * Created by Studio on 2017/6/22.
 *
 */
public class Pumelo {

    private String appkey;
    private String skey;


    private static Pumelo instance;
    private Bug bug;
    private RemoteProperties remoteProperties;


    private static boolean showBanner = true;
    private static boolean showLogs = true;


    private Pumelo(String appkey, String skey) {
        this.appkey = appkey;
        this.skey = skey;
        this.remoteProperties = new RemoteProperties(this);
        this.bug = new Bug(this);
    }

    public static void showBanner(boolean show)
    {
        showBanner = show;
    }

    public static void showLogs(boolean show)
    {
        showLogs = show;
    }


    public synchronized static void init(String appkey, String skey) throws Exception {
        if(appkey == null)
        {
            throw new NullPointerException("appkey can not null");
        }
        if(skey == null)
        {
            throw new NullPointerException("skey can not null");
        }


        if(instance == null)
        {
            if(showBanner)
            {
                showBanner();
            }

            instance = new Pumelo(appkey,skey);
            if(showLogs) System.out.println("Pumelo SDK init successfully");

        }
    }


    private static void showBanner()
    {
        System.out.println("");
        System.out.println(" _____   _   _       ___  ___   _____   _       _____  ");
        System.out.println("|  _  \\ | | | |     /   |/   | | ____| | |     /  _  \\ ");
        System.out.println("| |_| | | | | |    / /|   /| | | |__   | |     | | | | ");
        System.out.println("|  ___/ | | | |   / / |__/ | | |  __|  | |     | | | | ");
        System.out.println("| |     | |_| |  / /       | | | |___  | |___  | |_| | ");
        System.out.println("|_|     \\_____/ /_/        |_| |_____| |_____| \\_____/ ");
        System.out.println("Pumelo SDK                              v1.2 Snapshot");
        System.out.println("");
        System.out.println("");

    }



    public static void Report(Exception exception) throws Exception {
        if(instance == null)
        {
            throw new NullPointerException("Pumelo SDK uninitialized");
        }
        instance.bug.Report(exception);
    }

    public static void Report(String title,String content) throws Exception {
        if(instance == null)
        {
            throw new NullPointerException("Pumelo SDK uninitialized");
        }
        instance.bug.Report(title,content);
    }


    public static String[] LoadRemoteProperties(String version,String prefix)
    {
        if(instance == null)
        {
            throw new NullPointerException("Pumelo SDK uninitialized");
        }
        return instance.remoteProperties.LoadProperty(version,prefix);
    }


    public static boolean isShowBanner() {
        return showBanner;
    }

    public static boolean isShowLogs() {
        return showLogs;
    }

    public String getAppkey() {
        return appkey;
    }

    public String getSkey() {
        return skey;
    }
}
